<template>
  <basic-container>
    <avue-crud :before-open="beforeOpen"
               :data="data"
               :option="option"
               :page.sync="page"
               :permission="permissionList"
               :table-loading="loading"
               @row-update="rowUpdate"
               @current-change="currentChange"
               @on-load="onLoad"
               @refresh-change="refreshChange"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @size-change="sizeChange"
               ref="crud"
               v-model="form">
    </avue-crud>
    <template v-slot:status="{row}">
      <el-tag :type="row.status == '1'? 'info' : row.status == '2'?'success': row.status == '3'?'danger':''"
              effect="dark">{{ row.$status }}
      </el-tag>
    </template>
  </basic-container>
</template>

<script>
import {getDetail, getList, update} from "@/api/enterprise/policyapply";
import {mapGetters} from "vuex";
import {baseUrl} from "@/config/env";

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        viewBtn: true,
        saveBtn: false,
        updateBtn: false,
        cancelBtn: false,
        menuWidth: 130,
        dialogClickModal: false,
        dialogMenuPosition: "center",
        viewTitle: " ",
        labelWidth: 150,
        column: [
          {
            label: "企业名称",
            prop: "companyName",
            display: false,
            hide: true
          },
          {
            label: "政策名称",
            prop: "policyTitle",
            display: false,
            width: 550,
            overHidden: true
          },
          {
            label: "申报资金(万元)",
            prop: "money",
            editDisabled: true,
            display: false
          },
          {
            label: "申请人姓名",
            prop: "applicantName",
            editDisabled: true,
            display: false
          },
          {
            label: "申请状态",
            prop: "status",
            type: "radio",
            display: false,
            slot:true,
            dicData: [
              {
                label: "待审核",
                value: 1
              },
              {
                label: "通过",
                value: 2
              },
              {
                label: "驳回",
                value: 3
              }]
          },
        ],
        group: [
          {
            label: "基本信息",
            arrow: false,
            column: [
              {
                label: "企业名称",
                prop: "companyName",
                hide: true,
                editDisabled: true
              },
              {
                label: "政策名称",
                prop: "policyTitle",
                hide: true,
                editDisabled: true
              },
              {
                label: "银行卡号",
                prop: "cardNo",
                hide: true,
                editDisabled: true
              },
              {
                label: "银行名称",
                prop: "bank",
                hide: true,
                editDisabled: true
              },
              {
                label: "申报资金(万元)",
                prop: "money",
                editDisabled: true,
              },
              {
                label: "邮箱",
                prop: "email",
                hide: true,
                editDisabled: true
              },
              {
                label: "申请人姓名",
                prop: "applicantName",
                editDisabled: true
              },
              {
                label: "申请人电话",
                prop: "applicantPhone",
                hide: true,
                editDisabled: true
              },
              {
                label: "项目主要内容",
                prop: "content",
                hide: true,
                editDisabled: true
              }
            ]
          },
          {
            label: "附件",
            arrow: false,
            column: [
              {
                labelWidth: 0,
                prop: "files",
                type: "dynamic",
                span: 24,
                children: {
                  align: 'center',
                  column: [
                    {
                      label: "材料名",
                      prop: "name",
                    },
                    {
                      label: "文件",
                      prop: "url",
                      type: "upload",
                      dataType: "string",
                      action: baseUrl + '/blade-resource/local/endpoint/arrayFileUpload-file',
                      propsHttp: {
                        res: 'data'
                      },
                      uploadPreview: (file) => {
                        window.open(file.url, '_blank')
                      },
                    }
                  ]
                },
              }
            ]
          }
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission", 'userInfo']),
    permissionList() {
      return {
        addBtn: false,
        viewBtn: true,
        delBtn: false,
        editBtn: false
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    rowUpdate(row, index, done, loading) {
      update(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        loading();
        console.log(error);
      });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query, {enterprise: this.userInfo.account})).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style>
</style>
